/**
 * виджет реплик
 */

;(function($){

// вернуть случайное число в интервале от 0 до N
  $.extend(Math, {rand: function(n){return this.floor(this.random()*n);}});

  $.widget("fs.replics", {
    options : {
      replica : ".replica",
      url     : "",
      duration: "slow",
      top     : 0,
      minWidth: 0,
      time    : 1500,
      children: null,
      content : null
    },
  // конструктор
    _create : function()
              {
                var self = $.extend(this, {
                  _top           : this.element.offset().top,
                  _height        : this.element.height(),
                  _timer         : 0,
                  _replics       : [],
                  replics        : this.element.find(this.options.replica),
                  _replicaOnClick: function(){self._replica($(this));return false;},
                  _displayOnClick: function()
                                   {
                                     var $this = $(this);
                                     if (!(self._timer || self._replics.length))
                                     {
                                       $.ajax({url: self.options.url, type: "get", dataType: "xml", async: false, success: function(list)
                                       {
                                         self._replics = $.makeArray($(list).find("replic").map(function(){return $(this).text().replace(" - ", "&nbsp;&mdash; ");}));
                                         $this.
                                           height(self._height).
                                           add(self.options.children).
                                           css("background-position", "left top").
                                           end().
                                           add(self.options.content).
                                           css("position", "relative").
                                           animate({top: 0}, {
                                             complete: function()
                                                       {
                                                         if (this == $this[0])
                                                           self._timer = setInterval(function()
                                                           {
                                                             $(self.replics[Math.rand(self.replics.length)]).fadeOut(self.options.duration, function()
                                                             {
                                                               var $this = $(this);
                                                               var $text = $this.find("div");
                                                               var n     = Math.rand(self._replics.length);
                                                               var s     = self._replics[n];
                                                               if (s)
                                                               {
                                                                 var t = $text.html();
                                                                 self._replics.splice(n, 1, t ? t : null);
                                                                 $text.html(s);
                                                                 self._replica($this.css("top", self.options.top - $this.height() + parseInt($this.data("top")))).
                                                                   fadeIn(self.options.duration);
                                                               }
                                                             });
                                                           }, self.options.time);
                                                       },
                                             duration: self.options.duration
                                           });
                                       }});
                                       return false;
                                     }
                                   },
                  _bodyOnClick   : function(){self._hideAll();},
                  _wndResize     : function(){self._resize();}
                });
                this._ajust();
                this.replics.click(this._replicaOnClick);
                this.element.click(this._displayOnClick);
                $(document.body).click(this._bodyOnClick);
                $(window).resize(this._wndResize);
                this._resize();
              },
  // деструктор
    destroy : function()
              {
                $(window).unbind("resize", this._wndResize);
                $(document.body).unbind("click", this._bodyOnClick);
                this.element.unbind("click", this._displayOnClick);
                this.replics.unbind("click", this._replicaOnClick);
              },
  // поместить реплику поверх остальных
    _replica: function($replica){this.replics.css("z-index",1);return $replica.css("z-index",2);},
  // изменение размера окна браузера
    _resize : function()
              {
                var c = this.element.width() >> 1;
                var w = this.options.minWidth;
                this.replics.each(function(){with($(this))css("left",c-w+parseInt(data("left")));});
              },
  // выровнять границы
    _ajust  : function()
              {
                this.element.
                  height(this._height + this._top).
                  add(this.options.children).
                  css("background-position", ["left ", "px"].join(this._top)).
                  end().
                  add(this.options.content).
                  css("position", "static");
              },
  // скрыть дисплей
    _hideAll: function()
              {
                if (this._timer)
                {
                  clearInterval(this._timer);
                  var self = $.extend(this, {_timer: 0, _replics: []});
                  this.replics.hide().find("div").empty();
                  this.element.add(this.options.content).animate({top: this._top}, {
                    complete: function(){self._ajust();},
                    duration: this.options.duration
                  });
                }
              }
  });

})(jQuery);
